package com.tianitan.gen.controller;

import com.tianitan.gen.common.PageResult;
import com.tianitan.gen.common.ResponseResult;
import com.tianitan.gen.model.entity.User;
import com.tianitan.gen.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户信息存储表(User)控制层
 *
 * @author tiantian
 * @since 2023-09-24 15:46:31
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 通过主键查询单条数据
     *
     * @param uid 主键
     * @return 单条数据
     */
    @GetMapping("/get")
    public ResponseResult<User> selectOneById(@RequestParam("uid") Integer uid) {
        return userService.selectById(uid);
    }

    /**
     * 新增一条数据
     *
     * @param user 实体类
     * @return ResponseResult对象
     */
    @PostMapping("/add")
    public ResponseResult<Boolean> insert(@RequestBody User user) {
        return userService.insert(user);
    }

    /**
     * 修改一条数据
     *
     * @param user 实体类
     * @return ResponseResult对象
     */
    @PutMapping("/update")
    public ResponseResult<Boolean> update(@RequestBody User user) {
        return userService.updateById(user);
    }

    /**
     * 删除一条数据
     *
     * @param uid 主键
     * @return Boolean是否删除成功
     */
    @DeleteMapping("/del")
    public ResponseResult<Boolean> delete(Integer uid) {
        return userService.deleteById(uid);
    }

    /**
     * 分页查询
     *
     * @param pageNum  查询起始位置
     * @param pageSize 查询条数
     * @param user     实体筛选条件
     * @return ResponseResult<PageResult < User>>
     */
    @GetMapping("/page")
    public ResponseResult<PageResult<User>> selectPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            User user) {
        return userService.selectByPage(pageNum, pageSize, user);
    }

}

